package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.CarDao;
import cn.topevery.garden.entity.po.Car;
import cn.topevery.garden.entity.query.CarQuery;
import cn.topevery.garden.entity.vo.CarVO;
import cn.topevery.garden.entity.vo.oneMap.CountVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class CarDaoImpl extends BaseDaoImpl<Car> implements CarDao {
    @Override
    public <P extends DbCsp> List<Car> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<Car> listPage(CarQuery query) {
        String table = "t_car tc " +
                "left join t_task_info ta on ta.c_id = tc.c_task_id " +
                "left join t_dingtalk_duty du on du.c_id = tc.c_shift_id " +
                "left join t_extent ex on ex.c_id = tc.c_extent_id ";

        String columns = "ex.c_name extentNameT,ta.c_name taskName,du.c_name shiftName,tc.* ";
		int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tc.c_db_create_date desc");
        sqlQueryMate
                .where(true, "tc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotEmpty(query.getCode()),"tc.c_car_num",ScriptConditionEnum.Like,query.getCode())
                .where(StringUtils.isNotEmpty(query.getName()),"tc.c_car_num",ScriptConditionEnum.Eq,query.getName())
                .where(query.getStatus()!=null,"tc.c_work_state",ScriptConditionEnum.Eq,query.getStatus())
                .where(query.getParkId()!=null,"tc.c_park_id",ScriptConditionEnum.Eq,query.getParkId())
                .where(query.getSectionId()!=null,"tc.c_section_id",ScriptConditionEnum.Eq,query.getSectionId())
                .where(query.getExtentId()!=null,"tc.c_extent_id",ScriptConditionEnum.Eq,query.getExtentId())
                .where(query.getTaskId()!=null,"tc.c_task_id",ScriptConditionEnum.Eq,query.getTaskId())
                .where(query.getShiftId()!=null,"tc.c_shift_id",ScriptConditionEnum.Eq,query.getShiftId())
                .where(StringUtils.isNotBlank(query.getDeptId()), "tc.c_company_id", ScriptConditionEnum.Eq, query.getDeptId())

        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, Car.class);
    }

    /**
     * 描述 获取统计数量
     *
     * @param name     the name
     * @param extentId the extent id
     * @return the count
     * @author wenzhao.lin
     * @created 2021 -08-25 09:24:05
     */
    @Override
    public Map getCount(String name, Integer extentId, Integer typeId) {
        String table = "t_car tp";

        String columns = "count(1) value,'车辆总数' as name";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(name), "tp.c_car_num", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_section_id", ScriptConditionEnum.Eq, extentId)
                .where(typeId != null, "tp.c_type", ScriptConditionEnum.Eq, typeId)
        ;
        return getOneMap(sqlQueryMate);
    }

    /**
     * 描述 通过类型获取列表
     *
     * @param name       the name
     * @param extentId   the extent id
     * @param type the type
     * @return the person list by type
     * @author wenzhao.lin
     * @created 2021 -08-25 09:25:36
     */
    @Override
    public List<Car> getCarListByType(String name, Integer extentId, Integer type) {
        String table = "t_car tp";

        String columns = "tp.*, tp.c_car_num name";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(name), "tp.c_car_num", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_extent_id", ScriptConditionEnum.Eq, extentId)
                .where(type != null, "tp.c_type", ScriptConditionEnum.Eq, type)
        ;
        return selectList(sqlQueryMate, Car.class);
    }

    /**
     * 描述 获取类型列表
     *
     * @param name     the name
     * @param extentId the extent id
     * @return the person type list
     * @author wenzhao.lin
     * @created 2021 -08-27 09:15:52
     */
    @Override
    public List<Car> getCarTypeList(String name, Integer extentId) {
        String table = "t_car tp";

        String columns = "distinct tp.c_type, tp.c_type_name";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(name), "tp.c_car_num", ScriptConditionEnum.Like, name)
                .where(extentId != null, "tp.c_section_id", ScriptConditionEnum.Eq, extentId)
        ;

        List<Car> list = selectList(sqlQueryMate, Car.class);
        for(Car car : list){
            if(car.getType() != null){
                Map map = getCount(name, extentId, car.getType());
                car.setTotalNumber(String.valueOf(map.get("value")));
            }
        }

        return list;
    }

    /**
     * 描述 一张图管理-人车概况- 获取车辆统计
     *
     * @return the type count
     * @author wenzhao.lin
     * @created 2021 -09-07 16:54:23
     */
    @Override
    public List<CountVO> getCarCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) value, '车辆总数' name,'辆' unit from t_car where c_db_status=0 " +
                "union all " +
                "select count(1) value, '当班车辆' name,'辆' unit from t_curr_status_car t " +
                "inner join t_car p on p.c_car_num = t.c_number  " +
                "where t.c_db_status=0 and t.c_online_type =1 and p.c_db_status=0");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    /**
     * 描述 一张图管理-人车概况- 获取所有车辆类型统计
     *
     * @return the type count
     * @author wenzhao.lin
     * @created 2021 -09-07 16:54:23
     */
    @Override
    public List<CountVO> getCarTypeCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) value,c_type_name name,'辆' unit from t_car where c_db_status=0 and c_type_name is not null group by c_type_name order by value desc");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    /**
     * 描述 一张图管理-人车概况- 获取在岗人员类型统计
     *
     * @return the type count
     * @author wenzhao.lin
     * @created 2021 -09-07 16:54:23
     */
    @Override
    public List<CountVO> getCarOnlineCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) value,p.c_type_name name,'辆' unit from t_curr_status_car t " +
                "inner join t_car p on p.c_car_num = t.c_number  " +
                "where t.c_db_status=0 and t.c_online_type =1 and p.c_db_status=0 group by p.c_type_name order by value desc");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    /**
     * 描述 一张图管理-人车概况- 获取车辆 在线/离线 统计
     *
     * @return the car online count
     * @author wenzhao.lin
     * @created 2021 -09-13 09:19:32
     */
    @Override
    public List<CountVO> getCarOnlineAndOutCount() {
        StringBuffer sb = new StringBuffer();
        sb.append("select count(1) value, '在线' name,'辆' unit from t_curr_status_car t  " +
                "                inner join t_car p on p.c_car_num = t.c_number  " +
                "                where t.c_db_status=0 and t.c_online_type =1 and p.c_db_status=0 " +
                "union all " +
                "select count(1) value, '离线' name,'辆' unit from t_curr_status_car t  " +
                "                inner join t_car p on p.c_car_num = t.c_number  " +
                "                where t.c_db_status=0 and t.c_online_type =0 and p.c_db_status=0");

        AtomicReference<List<CountVO>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CountVO.class));
        });

        return vo.get();
    }

    /**
     * 描述 一张图管理-人车概况- 获取车辆列表
     *
     * @param query the query
     * @return the car list data
     * @author wenzhao.lin
     * @created 2021 -09-10 18:03:08
     */
    @Override
    public DataPager<CarVO> getCarListData(CarQuery query) {
        String table = "t_car p left join t_curr_status_car t on t.c_number = p.c_car_num ";

        String columns = "p.*,t.c_geo_x,t.c_geo_y,t.c_gps_id,t.c_client_key,t.c_online_type,t.c_online_time,t.c_valid_time,t.c_valid_mileage ";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("p.c_db_create_date desc");
        sqlQueryMate
                .where(true, "p.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getType()!=null && query.getType()==1 , "t.c_online_type", ScriptConditionEnum.Eq, query.getType())
                .where(StringUtils.isNotEmpty(query.getCode()),"p.c_car_num",ScriptConditionEnum.Like,query.getCode())
                .where(query.getStatus()!=null,"p.c_work_state",ScriptConditionEnum.Eq,query.getStatus())
                .where(query.getParkId()!=null,"p.c_park_id",ScriptConditionEnum.Eq,query.getParkId())
                .where(query.getSectionId()!=null,"p.c_section_id",ScriptConditionEnum.Eq,query.getSectionId())
                .where(query.getExtentId()!=null,"p.c_extent_id",ScriptConditionEnum.Eq,query.getExtentId())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, CarVO.class);
    }

    @Override
    public CarVO getByCarId(String carId) {
        String table = "t_car tc";

        String columns = "tc.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "tc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tc.c_car_id", ScriptConditionEnum.Eq, carId);
        AtomicReference<CarVO> car = new AtomicReference<>(new CarVO());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            car.set(dbCommand.executeToEntity(CarVO.class));
        });
        return car.get();
    }
}
